package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
   private TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);

    public void start() {
        lo:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    // System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    changeTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    searchTeacher();
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    //修改
    public void changeTeacher() {
        Teacher[] teachers = teacherService.useTeacher();
        if (teachers == null) {
            System.out.println("暂无信息，请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入要修改的学号或按N/n键返回上一级菜单");
            String id = sc.next();
            if (id.equalsIgnoreCase("n")) {
                return;
            }
            boolean b = teacherService.exit(id);
            if (b) {
                Teacher te = scTeacher(id);
                teacherService.changeTeacher(te);
                System.out.println("修改成功");
                break;
            }
        }
    }

    //删除
    public void deleteTeacher() {
        Teacher[] teachers = teacherService.useTeacher();
        if (teachers == null) {
            System.out.println("暂无信息，请先添加");
            return;
        }
        while (true) {
            System.out.println("请输入您要删除的id或按N/n键返回上一级菜单");
            String id = sc.next();
            if (id.equalsIgnoreCase("n")) {
                return;
            }
            boolean b = teacherService.exit(id);
            if (b) {
                teacherService.deleteTeacher(id);
                System.out.println("删除成功");
                break;
            } else {
                System.out.println("查无此人，请重新输入");
            }
        }
    }

    //查
    public void searchTeacher() {
        Teacher[] t = teacherService.useTeacher();
        if (t == null) {
            System.out.println("暂无信息，请先添加");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        teacherService.searchTeacher();
    }

    //添加
    public void addTeacher() {
        String id;
        while (true) {
            System.out.println("请输入老师id或按N/n键返回上一级菜单");
            id = sc.next();
            if (id.equalsIgnoreCase("n")) {
                return;
            }
            boolean b = teacherService.exit(id);
            if (b) {
                System.out.println("该id已被占用，请重新输入");
            } else {
                break;
            }
        }
        Teacher te = scTeacher(id);
        boolean b = teacherService.addTeacher(te);
        if (b) {
            System.out.println("添加成功");
        } else {
            System.out.println("用户已满,添加失败");
        }
    }

    public Teacher scTeacher(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        String age = sc.next();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher te = new Teacher(id, name, age, birthday);
        return te;
    }
}
